package com.pug.zixun.controller.coupon;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pug.zixun.service.coupon.ICouponService;
import com.pug.zixun.pojo.Coupon;
import com.pug.zixun.vo.CouponVo;
import com.pug.zixun.bo.CouponBo;
import com.pug.zixun.commons.enums.ResultStatusEnum;
import com.pug.zixun.commons.ex.PugValidatorException;
import com.pug.zixun.commons.utils.fn.asserts.Vsserts;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.pug.zixun.controller.BaseController;
import java.util.List;
import org.pug.generator.anno.PugDoc;
/**
 * CouponController
 * 创建人:yykk<br/>
 * 时间：2022-09-12 15:00:59 <br/>
 * 源码下载：前台代码 git clone https://gitee.com/kekesam/kuangstudy-pug-ui.git
 * 飞哥B站地址：后台代码 git clone https://gitee.com/kekesam/kuangstudy-pug-parent.git
 * @version 1.0.0<br/>
 *
*/

@RestController
@RequiredArgsConstructor
@Slf4j
@PugDoc(name="优惠卷管理",tabname="kss_coupon")
public class CouponController extends BaseController{

    private final ICouponService couponService;


    /**
     * 查询优惠卷管理列表信息
     * @path : /admin/coupon/list
     * @method: findCoupons
     * @result : List<CouponBo>
     * 创建人:yykk
     * 创建时间：2022-09-12 15:00:59
     * @version 1.0.0
     * @return
     */
    @PostMapping("/coupon/list")
    @PugDoc(name="查询优惠卷管理列表信息")
    public List<CouponBo> findCouponList() {
        return couponService.findCouponList();
    }

	/**
	 * 查询优惠卷管理列表信息并分页
	 * @path : /admin/coupon/load
     * @method: findCoupons
     * @param : couponVo
     * @result : IPage<CouponBo>
	 * 创建人:yykk
	 * 创建时间：2022-09-12 15:00:59
	 * @version 1.0.0
	*/
    @PostMapping("/coupon/load")
    @PugDoc(name="查询优惠卷管理列表信息并分页")
    public IPage<CouponBo> findCoupons(@RequestBody CouponVo couponVo) {
        return couponService.findCouponPage(couponVo);
    }

    @PostMapping("/coupon/update")
    public boolean update(@RequestBody Coupon coupon){
        return couponService.updateById(coupon);
    }

    /**
     * 根据优惠卷管理id查询明细信息
     * @method: get/{id}
     * @path : /admin/coupon/get/{id}
     * @param : id
     * @result : CouponBo
     * 创建人:yykk
     * 创建时间：2022-09-12 15:00:59
     * @version 1.0.0
    */
    @PostMapping("/coupon/get/{id}")
    @PugDoc(name="根据优惠卷管理id查询明细信息")
    public CouponBo getCouponById(@PathVariable("id") String id) {
        if(Vsserts.isEmpty(id)){
           throw new PugValidatorException(ResultStatusEnum.ID_NOT_EMPTY);
        }
        return couponService.getCouponById(new Long(id));
    }


	/**
	 * 保存和修改优惠卷管理
     * @method: saveupdate
	 * @path : /admin/coupon/save
     * @param : coupon
     * @result : CouponBo
	 * 创建人:yykk
	 * 创建时间：2022-09-12 15:00:59
	 * @version 1.0.0
	*/
    @PostMapping("/coupon/saveupdate")
    @PugDoc(name="保存和修改优惠卷管理")
    public CouponBo saveupdateCoupon(@RequestBody Coupon coupon) {
        return couponService.saveupdateCoupon(coupon);
    }


    /**
	 * 根据优惠卷管理id删除优惠卷管理
     * @method: delete/{id}
     * @path : /admin/coupon/delete/{id}
     * @param : id
     * @result : int
	 * 创建人:yykk
	 * 创建时间：2022-09-12 15:00:59
	 * @version 1.0.0
	*/
    @PostMapping("/coupon/delete/{id}")
    @PugDoc(name="根据优惠卷管理id删除优惠卷管理")
    public int deleteCouponById(@PathVariable("id") String id) {
        if(Vsserts.isEmpty(id)){
            throw new PugValidatorException(ResultStatusEnum.ID_NOT_EMPTY);
        }
        return couponService.deleteCouponById(new Long(id));
    }


   /**
   	 * 根据优惠卷管理ids批量删除优惠卷管理
     * @method: coupon/delBatch
     * @path : /admin/coupon/delBatch
     * @param : couponVo
     * @result : boolean
   	 * 创建人:yykk
   	 * 创建时间：2022-09-12 15:00:59
   	 * @version 1.0.0
   	*/
    @PostMapping("/coupon/delBatch")
    @PugDoc(name="根据优惠卷管理ids批量删除优惠卷管理")
    public boolean delCoupon(@RequestBody CouponVo couponVo) {
        log.info("你要批量删除的IDS是:{}", couponVo.getBatchIds());
        if (Vsserts.isEmpty(couponVo.getBatchIds())) {
            throw new PugValidatorException(ResultStatusEnum.ID_NOT_EMPTY);
        }
        return couponService.delBatchCoupon(couponVo.getBatchIds());
    }
    @PostMapping("/coupon/findByCourseId/{courseid}")
    public List<Coupon> findByCourseId(@PathVariable("courseid") Long courseid){
        LambdaQueryWrapper<Coupon> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Coupon::getType,1);
        lambdaQueryWrapper.eq(Coupon::getOpid,courseid);
        return couponService.list(lambdaQueryWrapper);
    }
    @PostMapping("/coupon/findByNoteId/{noteid}")
    public List<Coupon> findByNoteId(@PathVariable("noteid") Long noteid){
        LambdaQueryWrapper<Coupon> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Coupon::getType,2);
        lambdaQueryWrapper.eq(Coupon::getOpid,noteid);
        return couponService.list(lambdaQueryWrapper);
    }
}
